package org.jackysoft.entity;

import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.jackysoft.annotations.BID;
import org.jackysoft.util.MySqlSchema;
import org.jackysoft.util.NoSerialize;
import org.springframework.security.core.GrantedAuthority;

import com.google.common.collect.Lists;


@Entity
@Table(schema=MySqlSchema.Identifyedbase)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class AcegiRole  extends NoIdLREntity implements GrantedAuthority {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6202744855301590933L;
	private String id;
	private String name;	   
    private LoginPage loginIndex;  
    private AcegiRole parent;
    private boolean enabled = true;    
	
    
    private Collection<TreeNode> treeNodes = Lists.newArrayList();
    public AcegiRole() {}

	public AcegiRole(String id,String name) {
		this.name = name;
		this.setId(id);
	}
	
	
	@Id@BID
	@GeneratedValue(generator = "acegi")
	@GenericGenerator(name = "acegi", strategy = "org.jackysoft.hibernate.AcegiRoleIdGenerator")
	public String getId() {	
		
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof AcegiRole))
			return false;
		AcegiRole o = (AcegiRole) obj;
		if (o.getName() == null && this.getName() == null)
			return false;
		return (o.getName().equals(this.getName()));

	}
	
	

	public void setTreeNodes(Collection<TreeNode> treeNodes) {
		this.treeNodes = treeNodes;
	}

	@ManyToMany(
	       targetEntity = TreeNode.class,
	       fetch=FetchType.LAZY
	)
	@JoinTable(schema=MySqlSchema.Identifyedbase)	
    @NoSerialize
    @JSON(serialize=false)
	public Collection<TreeNode> getTreeNodes() {
		return treeNodes;
	}

	@Override
	@Transient
	public String getAuthority() {
		return name;
	}

	
	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	

	@Transient
	public AcegiRole getParent() {
		return parent;
	}

	public void setParent(AcegiRole parent) {
		this.parent = parent;
	}
	
	
	@ManyToOne
	public LoginPage getLoginIndex() {
		return loginIndex;
	}

	public void setLoginIndex(LoginPage loginIndex) {
		this.loginIndex = loginIndex;
	}

	


	

}
